/*
 *@Description: 调用追书神器小说接口
 *@Author: panlua
 *@Date: 2019-06-29 13:32:24
*/

const api = require('../config/api')
const Router = require('koa-router')

const {bookInfoDB} = require('../db/model/index')
const router = new Router({
  prefix: '/book'
})

/*
 *@name: 带子分类的父分类
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-06-29 14:10:54
*/
router.get('/category', async(ctx) => {
  try {
    await AXIOS.get(api.category.category).then(res => {
      ctx.body = res
    })
  } catch (err) {
    ctx.body = {
      ok: false,
      msgContent: '服务器异常'
    }
  }
})

/*
 *@name: 分类详情: 带着书籍
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-06-29 14:11:27
*/
router.get('/categoryDetail', async(ctx) => {
  /*
   *@description:
   *@author: panlua
   *@date: 2019-06-29 14:24:44
   *@variable3: major 分类
   *@variable4: minor 子类
   *@variable5: start 页码
   *@variable6: limit 数量
  */
  const {major, minor, start, limit} = await ctx.query
  if (!major || !start | !limit) {
    ctx.body = {
      ok: false,
      msgContent: '参数不全'
    }
    return false
  }
  const searchObj = {majorCate: major}
  minor && (searchObj.minorCate = minor)
  try {
    const bookData = await bookInfoDB.find(searchObj, {tags: 0, buzz: 0, __v: 0}).skip((start - 1) * limit).limit(+limit).sort({'buzz': -1})
    const total = await bookInfoDB.find(searchObj).count()
    ctx.body = {
      books: bookData,
      total: total,
      ok: true
    }
  } catch (err) {
    ctx.body = {
      ok: false,
      msgContent: '服务器异常'
    }
  }
})

/*
 *@name: 获取书籍详情
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-07-05 16:01:39
*/

router.get('/detail', async(ctx) => {
  const id = ctx.query.id
  try {
    await AXIOS.get(`${api.book.bookDetail}/${id}`).then(res => {
      ctx.body = {
        ok: true,
        infoData: res,
      }
    })
  } catch (err) {
    ctx.body = {
      ok: false,
      msgContent: '服务器异常'
    }
  }
})

/*
 *@name: 获取小说章节
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-07-06 16:17:33
*/
router.get('/chapter', async(ctx) => {
  const id = ctx.query.id
  try {
    await AXIOS.get(`${api.book.bookSource}`, {
      params: {
        view: 'summary',
        book: id
      }
    }).then(async(res) => {
      const sourceId = res[0]._id
      await AXIOS.get(`${api.book.bookChapter}/${sourceId}`, {
        params: {
          view: 'chapters'
        }
      }).then(ress => {
        ctx.body = {
          ok: true,
          chapterData: ress.chapters
        }
      })
    })
  } catch (err) {
    ctx.body = {
      ok: false,
      msgContent: '服务器异常'
    }
  }
})

/*
 *@name: 获取小说章节内容
 *@param:{type} 
 *@description:
 *@return:
 *@author: panlua
 *@date: 2019-07-06 16:18:31
*/
router.get('/content', async(ctx) => {
  const link = ctx.query.link
  if (!link) {
    ctx.body = {
      ok: false,
      msgContent: '参数不全'
    }
    return
  }
  try {
    const url = `${api.book.chapterDetailContent}/${link}`
    await AXIOS.get(url).then(res => {
      ctx.body = res
    })
  } catch (err) {
    ctx.body = {
      ok: false,
      msgContent: '服务器异常'
    }
  }
})
module.exports = router
